﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

using System.Net.Sockets;

namespace GMA_CLIENT
{
    class Program
    {
        public static string __dns = string.Empty;
        public static int __port = 0;
        public static bool flock = false;

        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            

            //Console.WriteLine("Connecting.....");
        L011:
            try
            {
                
                //Transfering of data in the form of bytes 
                Console.Write("\r\nGMA QUERRY> ");
                String str = Console.ReadLine();

                if (str == "dns")
                {
                    Console.WriteLine("Config start!");
                    Console.Write("DNS > ");
                    __dns = Console.ReadLine();

                    Console.Write("Port ? ");
                    string _p = Console.ReadLine();
                    __port = Convert.ToInt32(_p);
                    str = string.Empty;
                    Console.Clear();
                }
                Console.WriteLine("Connecting....");
            L002:
                //Establishing the connection 

                TcpClient tcpclnt = new TcpClient();
                System.Threading.Thread.Sleep(1000);
                tcpclnt.Connect(__dns, __port);
                Console.WriteLine("\r\nConnected!");
                
                Console.Write("\r\nGMA QUERRY> ");

                //Using the network stream to send data
                NetworkStream nts = tcpclnt.GetStream();
                StreamReader strread = new StreamReader(nts);
                StreamWriter strwrite = new StreamWriter(nts);

                str = Console.ReadLine();
                

                try
                {
                    while (str != string.Empty)
                    {
                        switch(str){
                            case "login": 
                                {
                                    string passwd = Returnpassword();
                                    str = "login "+passwd;
                                    break;
                                }
                            case "passwd":
                                {
                                    string passwd = Returnpassword();
                                    str = "passwd " + passwd;
                                    break;
                                }
                            case "file":
                                {
                                    flock = true;
                                    str = "file open";
                                    break;
                                }
                            case "exit":
                                {
                                    Environment.Exit(0);
                                    break;
                                }
                        }

                        str += " ";

                        Stream stm = tcpclnt.GetStream();
                        ASCIIEncoding asen = new ASCIIEncoding();
                        byte[] ba = asen.GetBytes(str);
                        Console.WriteLine("\r\nTransmitting.....");
                        stm.Write(ba, 0, ba.Length);

                        if(str == "file open "){
                            Console.WriteLine("input path: ");
                            string shortFileName = Console.ReadLine();
                            byte[] fileNameByte = Encoding.ASCII.GetBytes(Path.GetFileName(shortFileName));
                            byte[] fileData = File.ReadAllBytes(shortFileName);
                            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
                            fileNameLen.CopyTo(clientData, 0);
                            fileNameByte.CopyTo(clientData, 4);
                            fileData.CopyTo(clientData, 4 + fileNameByte.Length);
                            NetworkStream networkStream = tcpclnt.GetStream();
                            networkStream.Write(clientData, 0, clientData.GetLength(0));
                            networkStream.Close();

                            //System.Threading.Thread.Sleep(2000);
                            
                        }

                        byte[] bb = new byte[100];
                        int k = stm.Read(bb, 0, 100);
                        Console.WriteLine("Result GMAS => \r\n");
                        StringBuilder bd = new StringBuilder();
                        for (int i = 0; i < k; i++)
                        {
                            bd.Append(Convert.ToChar(bb[i]).ToString());
                            //Console.Write(Convert.ToChar(bb[i]));
                        }
                        Console.Write(bd.ToString());
                        
                        //Sending a pre-stored text data
                        strwrite.WriteLine("Transmitting has been success!");
                        strwrite.Flush();
                    
                        tcpclnt.Close();
                    }
                }
                catch (Exception ex)
                {
                    goto L002;
                }


                //Stream stm = tcpclnt.GetStream();
                //ASCIIEncoding asen = new ASCIIEncoding();
                //byte[] ba = asen.GetBytes(str);
                //Console.WriteLine("Transmitting.....");
                //stm.Write(ba, 0, ba.Length);
                //byte[] bb = new byte[100];
                //int k = stm.Read(bb, 0, 100);
                //for (int i = 0; i < k; i++)
                //    Console.Write(Convert.ToChar(bb[i]));

                ////Using the network stream to send data
                //NetworkStream nts = tcpclnt.GetStream();
                //StreamReader strread = new StreamReader(nts);
                //StreamWriter strwrite = new StreamWriter(nts);
                ////Sending a pre-stored text data
                
                //string str1 = "goodboy";
                //strwrite.WriteLine(str1);
                //strwrite.Flush();

                ////Transfering of the file name using the streams
                //Console.WriteLine("\r\nEnter the file path");
                //string str2 = Console.ReadLine();
                //strwrite.WriteLine(str2);
                //strwrite.Flush();

                //tcpclnt.Close();

            }
            catch (Exception e)
            {
                goto L011;
                //Console.WriteLine("Error..... " + e.StackTrace);
            }
            goto L011;
        }

        public static string Returnpassword()
        {
            Console.Write("   Enter password:");
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    password += info.KeyChar;
                    info = Console.ReadKey(true);
                    Console.Write("*");
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        password = password.Substring
                        (0, password.Length - 1);
                    }
                    info = Console.ReadKey(true);
                }
            }
            return password;
        }


    }
}
